package com.kasax.skin.support.corel;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import com.kasax.skin.support.utils.LogUtils;

public class SkinResources {
    private static SkinResources instance;
    // 当前app的Resource对象
    private final Resources mAppResource;
    // 皮肤包的资源对象
    private Resources skinResource;
    // 皮肤包的名称
    private String skinPkgName;
    // 是否使用默认皮肤
    private boolean isDefaultSkin = true;

    private SkinResources(Context context) {
        mAppResource = context.getResources();
    }

    public static void init(Context context) {
        if (instance == null) {
            synchronized (SkinResources.class) {
                if (instance == null) {
                    instance = new SkinResources(context);
                }
            }
        }
    }

    public static SkinResources getInstance() {
        return instance;
    }

    /**
     * 可能是Color 也可能是drawable
     *
     * @param resId
     * @return
     */
    public Object getBackground(int resId) {
        String resourceTypeName = mAppResource.getResourceTypeName(resId);
        if (resourceTypeName.equals("color")) {
            return getClolor(resId);
        } else {
            return getDrawable(resId);
        }
    }

    public Drawable getDrawable(int resId) {
        if (isDefaultSkin) {
            return mAppResource.getDrawable(resId);
        }

        int skinResId = getIdentifier(resId);
        if (skinResId == 0) {
            return mAppResource.getDrawable(resId);
        }
        return skinResource.getDrawable(skinResId);
    }

    /**
     * 获取皮肤包的资源的id
     */
    public int getIdentifier(int resId) {
        if (isDefaultSkin) {
            return resId;
        }
        //在皮肤包中不一定就是 当前程序的 id
        //获取对应id 在当前的名称 colorPrimary
        //R.drawable.ic_launcher
        // 获取资源名称
        String resName = mAppResource.getResourceEntryName(resId); //ic_launcher   /colorPrimaryDark
        // 获取资源类型
        String resType = mAppResource.getResourceTypeName(resId); //drawable
        int skinResId = skinResource.getIdentifier(resName, resType, skinPkgName);
        return skinResId;
    }

    public int getClolor(int resId) {
        if (isDefaultSkin) {
            return mAppResource.getColor(resId);
        }

        int skinResId = getIdentifier(resId);
        if (skinResId == 0) {
            return mAppResource.getColor(resId);
        }
        return skinResource.getColor(skinResId);
    }

    public ColorStateList getColorStateList(int resId) {
        if (isDefaultSkin) {
            return mAppResource.getColorStateList(resId);
        }

        int skinResId = getIdentifier(resId);
        if (skinResId == 0) {
            return mAppResource.getColorStateList(resId);
        }
        return skinResource.getColorStateList(skinResId);
    }

    public void applySkin(Resources skinResources, String packageName) {
        this.skinResource = skinResources;
        this.skinPkgName = packageName;
        isDefaultSkin = TextUtils.isEmpty(skinPkgName) || skinResources == null;
    }

    public void reset() {
        skinResource = null;
        skinPkgName = "";
        isDefaultSkin = true;
    }

    /**
     * 根据字体ID来获取实际的字体对象
     *
     * @param skinTypefaceId
     * @return
     */
    public Typeface getTypeface(int skinTypefaceId) {
        String skinTypefacePath = getString(skinTypefaceId);
        if (TextUtils.isEmpty(skinTypefacePath)) {
            // 如果未获取到就使用系统的默认的字体
            return Typeface.DEFAULT;
        }
        try {
            if (isDefaultSkin) {
                return Typeface.createFromAsset(mAppResource.getAssets(), skinTypefacePath);
            }
            return Typeface.createFromAsset(skinResource.getAssets(), skinTypefacePath);
        } catch (Exception e) {
            LogUtils.e(getClass(), e);
        }
        return Typeface.DEFAULT;
    }

    private String getString(int skinTypefaceId) {
        try {
            // 使用默认皮肤
            if (isDefaultSkin) {
                // 使用App设置的属性值
                return mAppResource.getString(skinTypefaceId);
            }

            int skinResId = getIdentifier(skinTypefaceId);
            if (skinResId == 0) {
                // 使用App设置的属性值
                return mAppResource.getString(skinTypefaceId);
            }
            return skinResource.getString(skinResId);
        } catch (Resources.NotFoundException e) {
            LogUtils.e(getClass(), e);
        }
        return null;
    }
}
